const express = require('express')
const router = require('./router')
const fs = require('fs')
const https = require('https')
const bodyParser = require('body-parser') // 解析req参数的中间件
const cors = require('cors')  // 解决跨域错误的中间件

const app = express()

app.use(cors())
// 下面两行用于解析req参数和json格式的参数
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())
app.use('/', router)

const privateKey = fs.readFileSync('./https/9478918_health-changan.top.key', 'utf8')
const certificate = fs.readFileSync('./https/9478918_health-changan.top.pem', 'utf8')
const credentials = { key: privateKey, cert: certificate }
const httpsServer = https.createServer(credentials, app)
const SSLPORT = 18082


const server = app.listen(3000, () => {
  const { address, port } = server.address()
  console.log('Example app listening on http://%s:%s', address, port)
})

httpsServer.listen(SSLPORT, function() {
  console.log('HTTPS Server is running on: https://localhost:%s', SSLPORT)
})
